﻿using SuperMap.UI;
using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic;
using Ycjw.PipeLine.Map.Basic.Conditions;

namespace Ycjw.PipeLine.Map.Tools
{
    /****************************************************************************************/
    /*
    功    能:
    作    者: RenZachary
    联系方式: rzcgis@foxmail.com（QQ455745253）
    生成日期: 2021年3月24日
    修改日志: 
    版权说明: @2021 西安云创经纬信息科技有限公司 版权所有 All right reserved.
    注    意: 本内容仅限于云创经纬公司内部使用，禁止用于商业目的。
    */
    /****************************************************************************************/
    public class Conditioner
    {
        IMessageInterface m_MessageInterface;
        private static Conditioner m_Instance;
        SceneControl m_SceneControl;
        DockPanel m_DockPanel;
        SceneEx m_SceneEx;
        public void SetSceneEx(SceneEx sceneEx)
        {
            m_SceneEx = sceneEx;
        }
        public static Conditioner Instance
        {
            get
            {
                if(m_Instance == null)
                {
                    m_Instance = new Conditioner();
                }
                return m_Instance;
            }
        }

        public IMessageInterface MessageInterface { get => m_MessageInterface; set => m_MessageInterface = value; }

        public void SetSceneControl(SceneControl sceneControl)
        {
            m_SceneControl = sceneControl;
        }

        public void SetDockPanel(DockPanel dp)
        {
            m_DockPanel = dp;
        }
        Control Opened(Type type)
        {
            var fs = Application.OpenForms;
            foreach(Form form in fs)
            {
                if(form.GetType() == type)
                {
                    return form;
                }
                var cs = form.Controls.Find(type.Name, true);
                if (cs != null && cs.Length > 0)
                    return cs[0];
            }
            foreach(Control c in  this.m_SceneEx.Controls)
            {
                if(c.GetType() == type)
                {
                    return c;
                }
            }
            return null;
        }
        public void CreateCondition(string type)
        {
            if(m_SceneControl.Scene.Layers.Count == 0)
            {
                Sunny.UI.UIMessageTip.Show("请先打开管线工作空间");
                return;
            }
            var conditionFullName = "Ycjw.PipeLine.Map.Tools.Conditions." + type + ",Ycjw.PipeLine.Map";
            var cond = Type.GetType(conditionFullName);
            var openedControl = Opened(cond);
            if (openedControl != null)
            {
                openedControl.Focus();
                return;
            }
            if(cond == null)
            {
                Sunny.UI.UIMessageBox.Show(conditionFullName + " 未找到");
                return;
            }
            ConditionPanel cp = (ConditionPanel)Activator.CreateInstance(cond);
            cp.BaseConditions.Add("SceneControl", this.m_SceneControl);
            cp.BaseConditions.Add("DockPanel", m_DockPanel);
            cp.BaseConditions.Add("SceneEx", this.m_SceneEx);
            cp.BaseConditions.Add("IMessageInterface", this.m_MessageInterface);
            cp.CreateResultWindow();
            
            string formText;
            if (string.IsNullOrEmpty(cp.ConditionPanelName))
            {
                formText = "条件输入";
            }
            else
            {
                formText = cp.ConditionPanelName;
            }
            foreach(Form form in Application.OpenForms)
            {
                if(form is ConditionForm && form.Text == formText)
                {
                    form.Close();
                    break;
                }
            }

            if (cp is BasicToolBar)
            {
                m_SceneEx.AddToolBar(cp as BasicToolBar);
                return;
            }
            else
            {
                m_SceneEx.RemoveAllToolBar();
            }
            ConditionForm cf = new ConditionForm(cp)
            {
                Text = formText,
                DockPanel = this.m_DockPanel,
                MessageInterface = this.m_MessageInterface
            };

            if (cp.ShowModal())
            {
                cf.ShowDialog(System.Windows.Forms.Application.OpenForms[0]);
            }
            else
            {
                cf.Show();
            }
        }
    }
}
